﻿
CREATE PROCEDURE [dbo].[fares_FiltreazaListaRulaje] 
	@filtruPart varchar(8000),
	@filtruArt varchar(8000),
	@filtruCat varchar(8000),
	@filtruTip varchar(800),
	@delaData datetime,
	@laData datetime,
	@StocuriZero bit,
	@Comparatie smallint,
	@ValoareStoc decimal(25,5),
	@CodUnitate smallint
AS
BEGIN
	SET NOCOUNT ON;
    -- Insert statements for procedure here
    SELECT * FROM
    (SELECT  td.Denumire+'('+td.TipOperatie + ')' Denumire, art.IDArticol,docDet.ValoareUnitara,doc.IDDocument,
    art.Denumire DenumireArticol,tca.Denumire DenumireCategorie, 
    part.Denumire DenumirePartener,um.Denumire DenumireUM,
    doc.DataDoc,doc.NrDoc,docDet.Cantitate,docDet.ValoareTotala,docDet.ValoareTVA,Coalesce(doc.NrDocReceptie,'') NrDocReceptie,doc.DataDocReceptie,
    td.IDTipDocument
    FROM [Articole] art
    LEFT OUTER JOIN [DocumenteDetaliu] docDet ON docDet.IDArticol = art.IDArticol
    JOIN [Documente] doc ON doc.IDDocument = docDet.IDDocument
    JOIN [TipDocument] td ON doc.IDTipDocument = td.IDTipDocument
    JOIN [Parteneri] part ON doc.IDPartener = part.IDPartener
    JOIN [TipCategoriiArticole] tca ON art.IDCategorie = tca.IDCategorie
    JOIN UnitatiMasura um ON art.IDUnitateMasura = um.IDUnitateMasura
    WHERE 
    art.CodUnitate = @CodUnitate AND doc.DataDoc >=@delaData and doc.DataDoc<=@laData AND
    (LEN(LTRIM(RTRIM(@filtruArt)))=0 OR (art.IDArticol IN (SELECT fart.value FROM dbo.SplitIntParameters(@filtruArt,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruPart)))=0 OR (part.IDPartener IN (SELECT fart.value FROM dbo.SplitBigIntParameters(@filtruPart,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruCat)))=0 OR (tca.IDCategorie IN (SELECT CAST(fart.value as smallint) FROM dbo.SplitParameters(@filtruCat,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruTip)))=0 OR (td.IDTipDocument IN (SELECT CAST(fart.value as smallint) FROM dbo.SplitParameters(@filtruTip,',') fart))) 
    )
    v
     
END